package com.lucas.lai.state.machine.squirrel.sample.one.controller;

import com.lucas.lai.state.machine.squirrel.sample.one.context.OrderContext;
import com.lucas.lai.state.machine.squirrel.sample.one.envent.OrderEvent;
import com.lucas.lai.state.machine.squirrel.sample.one.machine.OrderStateMachineEngine;
import com.lucas.lai.state.machine.squirrel.sample.one.model.OrderDTO;
import com.lucas.lai.state.machine.squirrel.sample.one.status.OrderState;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lucas.lai
 * @date 2023/11/9
 */
@Slf4j
@RestController
@RequestMapping(value = "/order/")
public class OrderController {

    // 注入状态机
    @Autowired
    OrderStateMachineEngine orderStateMachineEngine;


    @RequestMapping("/test")
    public void test(){
        log.info("test请求进入......");
        OrderDTO orderDTO = new OrderDTO(OrderState.INIT);
        OrderContext orderContext = new OrderContext(orderDTO);
        // 启动状态机，触发某事件，发生状态变化
        orderStateMachineEngine.fire(OrderEvent.SUBMIT_ORDER,orderContext);
        log.info("test请求完成......");
    }

    @RequestMapping("/test/skip")
    public void testSkipStatus(){
        log.info("testSkipStatus请求进入......");
        OrderDTO orderDTO = new OrderDTO(OrderState.INIT);
        OrderContext orderContext = new OrderContext(orderDTO);
        // 启动状态机，触发某事件，发生状态变化
        orderStateMachineEngine.fire(OrderEvent.PAY,orderContext);
        log.info("testSkipStatus请求完成......");
    }
}
